//
//  ExciseViewController.m
//  StringRange
//
//  Created by DengTianran on 16/3/8.
//  Copyright © 2016年 DengTianran. All rights reserved.
//

#import "ExciseViewController.h"
#import "SubCell1.h"
#import "SubCell2.h"
#import "MainCell.h"
#import "EgCell.h"
@interface ExciseViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong)NSMutableArray *dataArray;
@property (nonatomic, assign)BOOL isEdit;
@end

static NSString *identifier1 = @"cell1";
static NSString *identifier2 = @"cell2";
@implementation ExciseViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"测试1324567";
    self.navigationItem.title = @"测试27654321";
    self.navigationController.navigationItem.title = @"测试3567890";

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"1"] style:UIBarButtonItemStylePlain target:self action:@selector(begin)];
    for (int i = 0; i < 50; i ++) {
        [self.dataArray addObject:[NSNumber numberWithInteger:i]];
    }
    NSLog(@"%@", _dataArray);
    
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(80, 0, 30, 30);
    button.backgroundColor = [UIColor redColor];
    [button addTarget:self action:@selector(cancle) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationController.navigationBar addSubview:button];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"SubCell1" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:identifier1];
    [self.tableView registerNib:[UINib nibWithNibName:@"SubCell2" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:identifier2];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"EgCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"egcell"];
}
- (void)begin
{
    [self.tableView setEditing:YES animated:YES];
    for (int i = 0; i < self.dataArray.count; i++) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
        [self.tableView selectRowAtIndexPath:indexPath
                                      animated:NO
                                scrollPosition:UITableViewScrollPositionNone];
    }
    
    
    NSLog(@"%ld", [[self.tableView indexPathsForVisibleRows] count]);
}

- (void)cancle
{
    NSLog(@"+++++++%@", _dataArray);

    for (NSIndexPath * indexPath in [self.tableView indexPathsForSelectedRows]) {
        NSLog(@"%ld", indexPath.row);
        [self.dataArray removeObject:[NSNumber numberWithInteger:indexPath.row]];
    }
 
    NSLog(@"*******%ld", _dataArray.count);
    //        //在使用beginUpdates的时候，必须保证数据是最新的数据。
    [self.tableView beginUpdates];
    [self.tableView deleteRowsAtIndexPaths:[self.tableView indexPathsForSelectedRows] withRowAnimation:UITableViewRowAnimationLeft];
    [self.tableView endUpdates];
    //
    [self.tableView setEditing:NO animated:YES];
    
//    [self.tableView reloadData];
}

- (NSMutableArray *)dataArray
{
    if (_dataArray == nil) {
        self.dataArray = [@[]mutableCopy];
    }
    return _dataArray;
}

#pragma mark - tableViewDelegate

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView.editing) {
         return UITableViewCellEditingStyleDelete| UITableViewCellEditingStyleInsert;
    }else
    {
        return UITableViewCellEditingStyleDelete;
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}

- (void)clickButton:(UIButton *)sender {
    
    NSLog(@"我是绿色")
    ;
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
//    
//    if (cell == nil) {
//        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
//    }
    MainCell * cell = nil;
    if (indexPath.row % 3 == 0) {
        
        cell = [tableView dequeueReusableCellWithIdentifier:identifier1 forIndexPath:indexPath];
        [(( SubCell1 *)(cell)).clickButton addTarget:self action:@selector(clickButton:) forControlEvents:UIControlEventTouchUpInside];
        
       
     
    }else
    {
        
        cell = [tableView dequeueReusableCellWithIdentifier:identifier2 forIndexPath:indexPath];
    }
    UIView * backView = [UIView new];
    backView.frame = cell.frame;
    backView.backgroundColor = [UIColor whiteColor];
    /**
     *  多选时, 可以设置cell的背景颜色
     */
    cell.multipleSelectionBackgroundView = backView;
    [cell configureCellWithString:[NSString stringWithFormat:@"%ld", indexPath.row]];
    
//    EgCell *cell = [tableView dequeueReusableCellWithIdentifier:@"egcell" forIndexPath:indexPath];
    return cell;
 
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle==UITableViewCellEditingStyleDelete)
    {
       
        if (self.dataArray.count)
        {
      
            [self.dataArray removeObject:[NSNumber numberWithInteger:indexPath.row]];
            [self.tableView beginUpdates];
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
             [self.tableView endUpdates];
        }
    
    }else
            
    {
        
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 150;
}


- (nullable NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewRowAction * first = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleNormal title:@"未读" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        NSLog(@"未读");
        [tableView setEditing:NO];
        
    }];
 
    
    UITableViewRowAction * second = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDefault title:@"删除" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath) {
        NSLog(@"删除");
        NSLog(@"%ld", indexPath.row);
        [self.dataArray removeObject:[NSNumber numberWithInteger:indexPath.row]];
        [tableView beginUpdates];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationLeft];
        [tableView endUpdates];
        //
        [tableView setEditing:NO animated:YES];
        

    }];
    return @[second, first];
}

//- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return @"删除";
//}


//- (void)setEditing:(BOOL)editing animated:(BOOL)animated
//{
//    [super setEditing:editing animated:animated];
//    if (editing) {
//        [self.tableView setEditing:editing animated:YES];
//    }else
//        
//    {
//        NSLog(@"+++++++%@", _dataArray);
//        for (NSIndexPath * indexPath in [self.tableView indexPathsForSelectedRows]) {
//            NSLog(@"%ld", indexPath.row);
//            [self.dataArray removeObjectAtIndex:indexPath.row];
//        }
//        //        [self.collectionAry  removeAllObjects];
////        self.collectionAry = [DataBaseHeader returnTableData];
////        
//        NSLog(@"*******%ld", _dataArray.count);
////        //在使用beginUpdates的时候，必须保证数据是最新的数据。
//        [self.tableView beginUpdates];
//        [self.tableView deleteRowsAtIndexPaths:[self.tableView indexPathsForSelectedRows] withRowAnimation:UITableViewRowAnimationNone];
//        [self.tableView endUpdates];
////
//        [self.tableView setEditing:editing animated:YES];
//        
//        [self.tableView reloadData];
//        
//        
//    }}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/



@end
